<?php

/***********************************************************************
 * PLAYLIST CONFIGURATION
 * Please don't use " (double quotes) or backslash them (like this: \").
 ***********************************************************************/

$author = "";
$author_email = "";
$author_website = "";
$playlist_img = "";
$title = "";
$description = "";
$cover_url = "http://cover.radioblogclub.com/?t=";
$buy_url = "http://store.radioblogclub.com/";
$allow_access_from_domain = "*";



/***********************************************************************
 * URL OF THE SOUND DIRECTORY
 * If your web hosting does not provide a valid SERVER_NAME 
 * you may overide it here.
 ***********************************************************************/

$web_path = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']);



/***********************************************************************
 * DON'T TOUCH THIS
 * radio.blog.playlist.3.0
 * 23.03.2007
 * www.radioblogclub.com
 ***********************************************************************/

// INIT
$oPlaylist = new Playlist();
$oPlaylist->file_path = dirname(__FILE__);

// EXTENSIONS
$fileExt[] = 'mp3';
$fileExt[] = 'rbs';
$fileExt[] = 'swf';
$fileExt[] = 'rbv';
$fileExt[] = 'flv';

// GET SOUNDS
$oPlaylist->getSounds();

// XML HEADER
header("Content-Type: text/xml; charset=utf-8");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
if(!$_GET['noxsl']) echo "<?xml-stylesheet href=\"template.xsl\" type=\"text/xsl\"?>\n";

// DISPLAY PLAYLIST IN XML
print_nl('<playlists');
print_nl('author= "'.htmlentities($author).'"');
print_nl('author_email="'.htmlentities($author_email).'"'); 
print_nl('author_website="'.htmlentities($author_website).'"');
print_nl('playlist_img="'.htmlentities($playlist_img).'"');
print_nl('title="'.htmlentities($title).'"');
print_nl('description="'.htmlentities($description).'"');
print_nl('cover_url="'.htmlentities($cover_url).'"'); 
print_nl('buy_url="'.htmlentities($buy_url).'"'); 
print_nl('allow_access_from_domain="'.htmlentities($allow_access_from_domain).'"');
print_nl('>');
$oPlaylist->displayPlaylistXML();
print_nl('</playlists>');


/***********************************************************************
 * PLAYLIST FUNCTIONS
 ***********************************************************************/

// PLAYLIST CLASS
class Playlist {

    function Playlist() {
        
    }

    // GET SOUNDS
    function getSounds() {
        global $fileExt;
        $this->sounds = browseFile($this->file_path.'/', $fileExt, 1);
		krsort($this->sounds);
    }
    
    // DISPLAY PLAYLIST IN XML
    function displayPlaylistXML() {
        global $fileExt,$web_path;

        if (count($this->sounds) > 0) {
            $playlistNode = '';
            
        	foreach($this->sounds as $key=>$value) {
        	    $trackMod = $key;
                $trackExt = findExt($value,$fileExt);
                $trackType = 'audio';
                if ($trackExt == 'flv' || $trackExt == 'rbv') $trackType = 'video';
        	    $trackName = utf8(basename($value, '.'.$trackExt));
        	    $playlist = utf8(basename(dirname($value)));
        	    $path = utf8($web_path.str_replace($this->file_path, '', dirname($value)).'/'.rawurlencode(basename($value)));
    
                $track[$playlist][] = '<track trackMod="'.$trackMod.'" type="'.$trackType.'" title="'.$trackName.'" path="'.$path.'" />';
        	}
        	
        	foreach($track as $playlist=>$tracks) {
        	    print_nl('<playlist title="'.$playlist.'">');
        	    foreach($tracks as $value) {
        	        print_nl($value);
                }
        	    print_nl('</playlist>');
        	}
        }
    }
}

// BROWSE FILE
function browseFile($src_dir, $match = '.', $recursive = 0) {
    
    $d = dir(realpath($src_dir));
    $files = array();
    while($file = $d->read()) {

        if ($file == '.' || $file == '..') continue;

        // DIR
        if ( is_dir("$src_dir$file/") ) {
            if ($recursive == 1) {
                $files = $files + browseFile("$src_dir$file/", $match, $recursive); // RECURSIVE PART
            }
        }

        // FILE
        if ( findExt($file,$match) ) {
			$filemod = filemtime($src_dir.$file);
            while( isset($files[$filemod]) ) $filemod++; 
            $files[$filemod] = $src_dir.$file;
        }
    }

    $d->close();
    return $files;
}

// CONVERT TO UTF8
function utf8($string) {
    $out = utf8_encode(str_replace("&","&amp;",$string));
    return $out;
}

// PRINT + NEW-LINE
function print_nl($string) {
    print($string)."\n";
}

// FIND EXT
function findExt($filename, $ext) {
    if (!is_array($ext)) $ext = array($ext); 
    $arrayFile = explode('.',$filename);
    if (!in_array(end($arrayFile),$ext)) return false;
    return end($arrayFile);
}

?>